#include <QImage>

#include <iostream>
#include <QtDebug>

// opencv
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

static std::string sBaseFolder = "../Data";
static void toGray()
{
    cv::Mat cvImg = cv::imread(sBaseFolder + "/photo.png", cv::IMREAD_COLOR);
    cv::Mat cvGray;

    cv::cvtColor(cvImg, cvGray, COLOR_BGR2GRAY);

    cout << "cvImg : " << cvImg.size() << endl;
    cout << "cvGray: " << cvGray.size() << endl;

    imwrite(sBaseFolder + "/gray.bmp", cvGray);
}
int main(int argc, char *argv[])
{
    //toGray();

    cv::Mat cvImg = cv::imread(sBaseFolder + "/gray.bmp", cv::IMREAD_GRAYSCALE);

    // 转成 QImage
    QImage qImg =QImage((unsigned char*)(cvImg.data),
                        cvImg.cols, cvImg.rows,
                        cvImg.cols*cvImg.channels(),
                        QImage::Format_Mono
                        // QImage::Format_Indexed8
                        );


    qDebug() << "data address: " << QString::asprintf("0x%x", cvImg.data) ;
    qDebug() << "qImg: " << QString::asprintf("0x%x", qImg.bits()) ;

    //cvImg.release();
    int y = qImg.height()/3;
    for (int i=0; i< qImg.width(); i++) {
        cout << "(" << y << ", " << i << "):" << qImg.pixel(y,i);
        //qImg.setPixel(a, i, 255);
        // qImg.setPixel(a, i, 255);
    }
    //qImg.setPixel(0, 0, RGB_MASK)

    imwrite("temp2.bmp", cvImg);
    return 0;
}
